package com.happyfamily.springboot.crowdsourcingplatform.controller.wang;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.UserStory;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.StoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 雇主故事的控制类
 */
@Controller
@Slf4j
public class StoryController
{
	@Autowired
	private StoryService storyServiceImpl;

	/**
	 * 前台展示雇主的故事列表
	 * @return
	 */
	@ApiOperation(value = "前台故事展示", notes = "前台故事展示")
	@OperationLogDetail(detail = "前台故事展示", level = 1, operationUnit = OperationUnit.SYSTEM, operationType = OperationType.SELECT)
	@GetMapping("/story/storyList")
	@ResponseBody
	public List<UserStory> storyList()
	{
		return storyServiceImpl.storyList();
	}

	/**
	 * 前台展示更多雇主故事
	 * @param request
	 * @param who
	 * @param starttime
	 * @param endtime
	 * @param stitle
	 * @param storySticky
	 * @return
	 */
	@ApiOperation(value = "前台雇主进行故事查看", notes = "前台雇主进行故事查看")
	@OperationLogDetail(detail = "前台雇主进行故事查看", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	@RequestMapping("/story/findStoryList")
	@ResponseBody
	public Results<UserStory> findStoryList(PageTableRequest request, String who, String starttime, String endtime, String stitle, String storySticky)
	{
		request.countOffset();
		return storyServiceImpl.findStoryList(request.getOffset(), request.getLimit(), who, starttime, endtime, stitle, storySticky);
	}

	/**
	 * 前台展示雇主自己的故事
	 * @param request
	 * @param starttime
	 * @param endtime
	 * @param stitle
	 * @param storySticky
	 * @param storyStatus
	 * @param scontent
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "前台雇主自己的故事查看", notes = "前台雇主自己的故事查看")
	@OperationLogDetail(detail = "前台雇主自己的故事查看", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	@RequestMapping("/api-front/story/myStoryList")
	@ResponseBody
	public Results<UserStory> myStoryList(PageTableRequest request, String starttime, String endtime, String stitle, String storySticky, String storyStatus, String scontent, HttpServletRequest req)
	{
		request.countOffset();
		return storyServiceImpl.myStoryList(request.getOffset(), request.getLimit(), starttime, endtime, stitle, storySticky, storyStatus, scontent, req);
	}

	/**
	 * 前台雇主发布故事
	 * @param titles
	 * @param contents
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "前台雇主发布故事", notes = "前台雇主发布故事")
	@OperationLogDetail(detail = "前台雇主发布故事", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.INSERT)
	@PostMapping("/api-front/story/storyAdd")
	@ResponseBody
	public String storyAdd(String titles, String contents, HttpServletRequest request)
	{
		return storyServiceImpl.storyAdd(titles, contents, request);
	}

	/**
	 * 前台雇主重新发布故事
	 * @param sid
	 * @return
	 */
	@ApiOperation(value = "前台雇主重新发布故事", notes = "前台雇主重新发布故事")
	@OperationLogDetail(detail = "前台雇主重新发布故事", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	@PostMapping("/api-front/story/story-resubmit")
	@ResponseBody
	public String resubmit(String sid)
	{
		return storyServiceImpl.resubmit(sid);
	}

	/**
	 * 跳转查看更多雇主故事窗口
	 * @return
	 */
	@ApiOperation(value = "跳转查看更多雇主故事窗口", notes = "跳转查看更多雇主故事窗口")
	@GetMapping("/story/moreStory")
	public String moreStory()
	{
		return "frontPage/story-list";
	}


	/**
	 * 跳转查看  雇主我的故事窗口
	 * @return
	 */
	@ApiOperation(value = "跳转查看雇主我的故事窗口", notes = "跳转查看雇主我的故事窗口")
	@GetMapping("/api-front/story/myStory")
	public String myStory()
	{
		return "frontPage/mystory-list";
	}

	/**
	 * 管理员页面展示故事表格
	 * @param request
	 * @param who
	 * @param starttime
	 * @param endtime
	 * @param storyStatus
	 * @param storySticky
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:query')")
	@ApiOperation(value = "管理员页面展示故事表格", notes = "管理员页面展示故事表格")
	@OperationLogDetail(detail = "管理员页面展示雇主故事表格", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-back/story/storyTable")
	@ResponseBody
	public Results<UserStory> storyTable(PageTableRequest request, String who, String starttime, String endtime, String storyStatus, String storySticky)
	{
		request.countOffset();
		return storyServiceImpl.storyTable(request.getOffset(), request.getLimit(), who, starttime, endtime, storyStatus, storySticky);
	}

	/**
	 * 管理员对故事的删除操作
	 * @param sid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:delete')")
	@ApiOperation(value = "管理员对故事的删除操作", notes = "管理员对故事的删除操作")
	@OperationLogDetail(detail = "管理员对故事的删除操作", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	@PostMapping("/api-back/story/story-delete")
	@ResponseBody
	public String storyDelete(String sid)
	{
		return storyServiceImpl.storyDelete(sid);
	}

	/**
	 * 管理员对故事的审核通过操作
	 * @param sid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:yes')")
	@ApiOperation(value = "管理员对故事的审核通过操作", notes = "管理员对故事的审核通过操作")
	@OperationLogDetail(detail = "管理员对故事的审核通过操作", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/story/story-yes")
	@ResponseBody
	public String storyYes(String sid)
	{
		return storyServiceImpl.storyYes(sid);
	}

	/**
	 * 管理员对故事的审核不通过操作
	 * @param sid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:no')")
	@ApiOperation(value = "管理员对故事的审核不通过操作", notes = "管理员对故事的审核不通过操作")
	@OperationLogDetail(detail = "管理员对故事的审核不通过操作", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/story/story-no")
	@ResponseBody
	public String storyNo(String sid)
	{
		return storyServiceImpl.storyNo(sid);
	}


	/**
	 * 管理员对故事的取消置顶操作
	 * @param sid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:cancelSticky')")
	@ApiOperation(value = "管理员对故事的取消置顶操作", notes = "管理员对故事的取消置顶操作")
	@OperationLogDetail(detail = "管理员对故事的取消置顶操作", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/story/story-cancelSticky")
	@ResponseBody
	public String cancelSticky(String sid)
	{
		return storyServiceImpl.cancelSticky(sid);
	}

	/**
	 * 管理员对故事的置顶操作
	 * @param sid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:story:doSticky')")
	@ApiOperation(value = "管理员对故事的置顶操作", notes = "管理员对故事的置顶操作")
	@OperationLogDetail(detail = "管理员对故事的置顶操作", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/story/story-doSticky")
	@ResponseBody
	public String doSticky(String sid)
	{
		return storyServiceImpl.doSticky(sid);
	}
}
